package com.dcrzl.shcz.module.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.shcz.common.entity.info.Gov;
import com.dcrzl.shcz.common.entity.info.GovUnit;

import com.dcrzl.shcz.common.mapper.info.GovUnitMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.info.service.GovService;
import com.dcrzl.shcz.module.info.service.GovUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

@Service("govUnitService")
public class GovUnitServiceImpl extends BaseServiceImpl<GovUnitMapper, GovUnit> implements GovUnitService {

    @Autowired
    private GovService govService;

    private LambdaQueryWrapper<GovUnit> commonWrapper(GovUnit govUnit) {
        LambdaQueryWrapper<GovUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(govUnit.getGovType()), GovUnit::getGovType, govUnit.getGovType());
        wrapper.eq(Objects.nonNull(govUnit.getId()), GovUnit::getId, govUnit.getId());
        wrapper.like(StringUtils.hasText(govUnit.getName()), GovUnit::getName, govUnit.getName());
        wrapper.eq(Objects.nonNull(govUnit.getStatus()), GovUnit::getStatus, govUnit.getStatus());
        wrapper.in(!CollectionUtils.isEmpty(govUnit.getIdList()), GovUnit::getId, govUnit.getIdList());
        wrapper.in(!CollectionUtils.isEmpty(govUnit.getIdListStr()),GovUnit::getId, govUnit.getIdListStr());
        return wrapper;
    }

    @Override
    public Result<GovUnit> selectOne(GovUnit govUnit) {
        LambdaQueryWrapper<GovUnit> wrapper = this.commonWrapper(govUnit);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<GovUnit>> selectList(GovUnit govUnit) {
        LambdaQueryWrapper<GovUnit> wrapper = this.commonWrapper(govUnit);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<GovUnit> selectPage(GovUnit govUnit) {
        LambdaQueryWrapper<GovUnit> wrapper = this.commonWrapper(govUnit);
        return super.selectPage(govUnit, wrapper);
    }

    @Override
    public PageInfo<GovUnit> selectUPage(GovUnit govUnit) {
        PageInfo<GovUnit> govUnitPageInfo = this.selectPage(govUnit);
        List<GovUnit> list = govUnitPageInfo.getList();
        Set<Long> govTypeList = new HashSet<>();
        list.forEach(govUnit1 -> {
            govTypeList.add(govUnit1.getGovType());
        });
        if(!CollectionUtils.isEmpty(govTypeList)){
            Result<List<Gov>> listResult = govService.selectList(new Gov().setIdList(govTypeList));
            if(listResult.isSuccess()){
                List<Gov> govList = listResult.getData();
                Map<Long,Gov> map = new HashMap<>();
                govList.forEach(gov -> {
                    map.put(gov.getId(),gov);
                });
                list.forEach(govUnit1 -> {
                    govUnit1.setGov(map.get(govUnit1.getGovType()));
                });
            }
        }
        return govUnitPageInfo;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        GovUnit govUnit = new GovUnit().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(govUnit);
    }

}